# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: test2.py
Author: kij
Create Date: 2021/3/7
-------------------------------------------------
"""
import time

# print(time.time())
# # 频率单位
# hz_dict = {}
# message = input('输入频率例如1/s:').split('/')
#
# hz_dict[message[1]] = int(message[0])
# print(hz_dict)
# # 姓名字典
# name_dict = {}
# print('开始测试')
# while True:
# 	name = input('name:')
# 	if name.upper() == 'Q':
# 		break
# 	if name not in name_dict:
# 		name_dict.setdefault(name, 1)
# 	else:
# 		name_dict[name] += 1
# 	print(name_dict[name])
# 	for key, value in name_dict.items():
# 		print('name=%s logon_time=%s' % (key, value))

# 所有用户访问记录
record = {
	"alex": [1615102211, 1615102211]
}

# 第一步：提示用户去配置频率（1/s、5/m）
rate = input("请输入限制频率：")
num, period = rate.split('/')
num_requests = int(num)
print(period[0])
duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]

# 第二步：用户可以开始访问
while True:
	name = input(">>>")  # alex  / eric
	# 最后一次的访问时间的秒数
	history = record.get(name, [])  # None
	# 当前时间
	ctime = time.time()

	while history and history[-1] <= ctime - duration:
		history.pop()
	# 访问的列表长度大于次数不让访问
	if len(history) >= num_requests:
		print("频率限制请稍等访问")
		continue
	if name in record:
		record[name].insert(0, ctime)
	else:
		record[name] = [ctime, ]
	print("访问成功", ctime)
